﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YAMP;

namespace SayisalAnaliz.Library
{
    public static class BiSectionYontemi
    {
        public static double[] Uygula(double baslangic, double bitis, double epsilon, string fonksiyon)
        {
            fonksiyon = fonksiyon.Replace(',', '.');
            double fa, fb, fc, a, b, c;
            a = baslangic;
            b = bitis;           
            do
            {
                c = (a + b) / 2;
                fa = ((ScalarValue)Parser.Parse(fonksiyon).Execute(new { x = a })).Value;
                fb = ((ScalarValue)Parser.Parse(fonksiyon).Execute(new { x = b })).Value;
                fc = ((ScalarValue)Parser.Parse(fonksiyon).Execute(new { x = c })).Value;
                if ((fa * fc) < 0)
                {
                    b = c;
                }

                if ((fa * fc) > 0)
                {
                    a = c;
                }
            } while (Math.Abs(fc) > epsilon);
            double[] sonuclar = new double[2];
            sonuclar[0] = c;
            sonuclar[1] = fc;
            return sonuclar;
        }
    }
}
